package com.kakaxi.mybilibili.module.home.live.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.LiveConsiderItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.live.model.ILiveConsiderModel;
import com.kakaxi.mybilibili.module.home.live.model.LiveConsiderModel;
import com.kakaxi.mybilibili.module.home.live.view.ILiveConsiderView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.List;

/**
 * 直播主播关注控制层实现类
 * Created by kakaxi on 16/11/13.
 */

public class LiveConsiderPresenter implements ILiveConsiderPresenter {

    /** 视图层 **/
    private ILiveConsiderView mView;

    /** 数据层 **/
    private ILiveConsiderModel mModel;

    public LiveConsiderPresenter(ILiveConsiderView liveConsiderView) {
        mView = liveConsiderView;
        mModel = new LiveConsiderModel();
    }

    @Override
    public void initLiveConsiderData() {
        mView.showLoading();
        convertLiveConsiderData();
    }

    @Override
    public void refreshLiveConsiderData() {
        convertLiveConsiderData();
    }

    private void convertLiveConsiderData() {
        mModel.getLiveConsiderData(new Listener<JSONObject>() {

            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();
                // 判断是否正确
                if (response.getString(WebParamConst.RESPONSE_CODE).equals
                        (WebParamConst.RESULT_CODE_OK)) {
                    // 获取数据,并把json转化为bean
                    String liveConsiderJsonArrStr = response.getJSONObject(WebParamConst
                            .RESPONSE_DATA).getJSONArray(WebParamConst
                            .RESPONSE_DATA_LIVE_CONSIDER_LIST).toString();
                    List<LiveConsiderItemBean> liveRoomGiftTopItemBeanList = JSON.parseArray
                            (liveConsiderJsonArrStr, LiveConsiderItemBean.class);
                    mView.showLiveConsiderData(liveRoomGiftTopItemBeanList);
                } else {
                    mView.showMessage(response.getString(WebParamConst
                            .RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }

}
